<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>博客正文</title>
    <link rel="stylesheet" href="css/conmmon.css">
    <link rel="stylesheet" href="css/blog_content.css">
    <link rel="stylesheet" href="editor.md/css/editormd.preview.min.css" />
    <style>
        .nav .spacer {
            width: 64%;
        }
        .nav a {
            display: flex;
            justify-content: center;
            align-items: center;
        }
        .nav a img {
            transition: transform .2s ease-in-out; /* 添加过渡效果，使图片放大更加平滑 */  
        }
        .nav a img:hover {  
            transform: scale(1.3) translateY(10px); /* 放大图片到1.5倍 */  
        }

    </style>
    <script src="js/jquery.min.js"></script>
    <script src="editor.md/editormd.js"></script>
    <script src="editor.md/lib/marked.min.js"></script>
    <script src="editor.md/lib/prettify.min.js"></script>
    <script src="js/common.js"></script>
</head>

<body>
    <!-- 导航栏 -->
    <div class="nav">
        <img src="img/logo2.png" alt="">
        <span class="title">我的博客系统</span>
        <!-- 用来占据中间位置 -->
        <span class="spacer"></span>
        <a href="myblog_list.html"><img src="img/default.jpg" alt=""></a>
        <a href="blog_list.html">主页</a>
        <a href="blog_edit.html">写博客</a>
        <a id="mybtn" href="login.html">登陆</a>
    </div>
    <!-- 版心 -->
    <div class="container">
        <!-- 左侧个人信息 -->
        <div class="container-left">
            <div class="card">
                <img id="photo" src="img/default.jpg" class="avtar" alt="">
                <h3 id="nickname">小可爱</h3>
                <a id="gitUrl" href="http:www.github.com">github 地址</a>
                <div class="counter">
                    <span>文章</span>
                    <span>分类</span>
                </div>
                <div class="counter">
                    <span>0</span>
                    <span>0</span>
                </div>
            </div>
        </div>
        <!-- 右侧内容详情 -->
        <div class="container-right">
            <div class="blog-content">
                <!-- 博客标题 -->
                <h3 id="title">我的第一篇博客</h3>
                <!-- 博客时间 -->
                <div class="date">
                    发布日期：<span id="date">xxxx-xx-xx</span>&nbsp;&nbsp;&nbsp;&nbsp;
                    访问量：<span id="rcount">0</span>
                </div>
                <!-- 博客正文 -->
                <div id="editorDiv">

                </div>
            </div>
            <div class="bottom-navbar">
                <div class="left-section">
                    <img src="img/default.jpg" alt="Author Avatar" class="author-avatar">
                    <span class="author-name">小可爱</span>
                </div>
                <div class="right-section">
                    <button class="btn">点赞</button>
                    <button class="btn">评论</button>
                    <button class="btn">收藏</button>
                    <button class="btn">分享</button>
                </div>
            </div>
        </div>
    </div>


    <script type="text/javascript">

        var editormd;
        function initEdit(md){
            editormd = editormd.markdownToHTML("editorDiv", {
            markdown : md, // Also, you can dynamic set Markdown text
            // htmlDecode : true,  // Enable / disable HTML tag encode.
            // htmlDecode : "style,script,iframe",  // Note: If enabled, you should filter some dangerous HTML tags for website security.
            });
        }

        function getLoginStatus() {
            jQuery.ajax({
                url: "/user/userstatus",
                type: "get",
                success: function(body) {
                    var mybtn = jQuery('#mybtn');
                    //未登录就更改按钮
                    if(body.data == 1) {
                        mybtn.text("注销");
                        mybtn.click(function() {
                            if(confirm("确认退出？")) {
                                jQuery.ajax({
                                    url: "/user/logout",
                                    type: "post",
                                    data: {},
                                    success: function(body) {
                                        location.href = "/login.html";
                                    },
                                    error: function(err) {
                                        alert("用户未登陆，即将跳转到登陆页！");
                                        location.href = "/login.html";
                                    }
                                });
                            }
                        })
                    }
                }
            })
        }
        getLoginStatus();

        //获取用户信息
        function getUserInfo(uid) {
            jQuery.ajax({
                url: "/user/userinfo",
                type: "get",
                data: {"uid": uid},
                success: function(body) {
                    if(body.code == 200 && body.data != null) {
                        //更改昵称
                        jQuery('#nickname').text(body.data.nickname);
                        if(body.data.photo != '') {
                            jQuery('#photo').url(body.data.photo);
                        }
                        if(body.data.gitUrl != '') {
                            jQuery('#gitUrl').href(body.data.gitUrl);
                        }
                    }
                },
                error: function(err) {
                    alert("用户未登陆，即将跳转到登陆页！");
                    location.href = "/login.html";
                }
            });
        }

        // 获取文章的详情信息
        function getArtDetail() {
            var id = getURLParam("id");
            if(id != null && id != "" && id > 0) {
                jQuery.ajax({
                    url:"/article/detail",
                    type:"POST",
                    data:{"id": id},
                    success:function(result) {
                        if (result.code == 200 && result.data != null) {
                            var art =result.data
                            jQuery('#title').text(art.title);
                            jQuery('#date').text(art.createTime);
                            jQuery('#rcount').text(art.readCount);
                            //使用editormd渲染文章内容
                            editormd.markdownToHTML('editorDiv', {
                                markdown: art.content
                            });
                            getUserInfo(art.uid);
                        }
                    }
                })
            }
        }

        getArtDetail();
    </script> 
</body>

</html>